#include <iostream>
#include <unistd.h>
#include "LeastSquareMethod.h"
#include "ResultCheck.h"

using namespace std;

// reference: https://blog.shipengx.com/archives/e0ebe48c.html

int main(int argc, char * argv[])
{
    int order = 3, opt = 0;   
    while((opt = getopt(argc, argv, "o:")) != -1) {
        switch(opt) {
            case 'o':
                order = atoi(optarg);
                break;
        }
    }              
    // float x[5] = {1, 2, 3, 4, 5};
    // float y[5] = {7, 35, 103, 229, 431};
    // float x[] = {50, 74, 98, 122, 144, 168, 192, 214, 236, 262,
    //              282, 306, 328, 356, 376, 400, 424, 446, 468};
    // float y[] = {120, 110, 100, 100, 80, 80, 70, 50, 40, 20,
    //              20, 30, 40, 50, 50, 50, 50, 40, 30};
    // float x[] = {100, 200, 300, 400, 450, 500, 600, 700};
    // float y[] = {300, 300, 300, 300, 350, 400, 400, 400};
    float x[] = {100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 450, 500, 600, 625, 650, 675, 700};
    float y[] = {300, 300, 300, 300, 300, 300, 300, 300, 300, 300, 300, 300, 300, 350, 400, 400, 400, 400, 400, 400};


    vector<float> X(x, x + sizeof(x) / sizeof(float));
    vector<float> Y(y, y + sizeof(y) / sizeof(float));

    cout << "\nThe coefficients vector is: \n" << endl;

    /*********  Implement with eigen *********/
    // Eigen::VectorXf result(FitterLeastSquareMethod(X, Y, order+1));

    // for (size_t i = 0; i < result.size(); ++i)
    //     cout << "theta_" << i << ": " << result[i] << endl;
    
    // vector<float> theta(result.data(), result.data()+result.rows()*result.cols());
   
    
    /*********  Implement with opencv *********/
    cv::Mat mat_k = polyfit(X, Y, order);
    std::cout << "mat_k:\n" << mat_k << "\n";
    vector<float> theta = vector<float>(mat_k.reshape(1, 1));


    ResutlCheck(X, Y, theta);

    return 0;
}
